package coml.jiyun.hp.ershi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.ershi.R;
import coml.jiyun.hp.ershi.bean.WanBean;

public class RlvHomeAdapter extends RecyclerView.Adapter {

    private Context context;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private int left = 1;
    private int right = 2;

    public RlvHomeAdapter(Context context, ArrayList<WanBean.DataBean.DatasBean> datasBeans) {
        this.context = context;
        this.datasBeans = datasBeans;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == left){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home_one, parent, false);
            return new ViewHolder1(inflate);
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_home_two, parent, false);
            return new ViewHolder2(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = holder.getItemViewType();
        WanBean.DataBean.DatasBean datasBean = datasBeans.get(position);
        if (itemViewType == left){
            ViewHolder1 holder1 = (ViewHolder1) holder;
            holder1.mItem_one_tv.setText(datasBean.getTitle());
        }else {
            ViewHolder2 holder2 = (ViewHolder2) holder;
            holder2.mItem_two_tv.setText(datasBean.getTitle());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.dj(position);
            }
        });


        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onItemLongClick != null){
                    onItemLongClick.ca(v,position);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position %2 ==0){
            return left;
        }else {
            return right;
        }
    }

    @Override
    public int getItemCount() {
        return datasBeans.size();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {

            ImageView mItem_one_img;
            TextView mItem_one_tv;

        public ViewHolder1(@NonNull View itemView) {
            super(itemView);

            mItem_one_img = itemView.findViewById(R.id.item_home_one_img);
            mItem_one_tv = itemView.findViewById(R.id.item_home_one_tv);

        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {

            TextView mItem_two_tv;
            ImageView mImg_two_img;

        public ViewHolder2(@NonNull View itemView) {
            super(itemView);

           mItem_two_tv = itemView.findViewById(R.id.item_home_two_tv);
           mImg_two_img = itemView.findViewById(R.id.item_home_two_img);

        }
    }

    public interface onItemClick{
        void dj(int i);
    }
    onItemClick onItemClick;

    public void setOnItemClick(RlvHomeAdapter.onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface onItemLongClick{
        void ca(View view,int i);
    }

    onItemLongClick onItemLongClick;

    public void setOnItemLongClick(RlvHomeAdapter.onItemLongClick onItemLongClick) {
        this.onItemLongClick = onItemLongClick;
    }
}
